import logging
from logging import debug
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)


import tables as tb
import numpy as np
import numpy.ma as ma
from numpy.lib import recfunctions
import matplotlib.pyplot as plt
from matplotlib.dates import num2date

def generate_time_seq():
    from dateutil import rrule
    import datetime
    d1 = datetime.datetime(2011, 01, 01, 0)
    d2 = datetime.datetime(2012, 01, 01, 0)
    list(rrule.rrule(rrule.MINUTELY, interval=10, dtstart=d1, until=d2))



def _find_common_time_base(data, describs):
    names = data.dtype.names

    time_names = []
    para_names = []
    for i, describ in enumerate(describs):
        para_short = describ[0:4]
        if "Time" == para_short:
            time_names.append(names[i])
        else:
            para_names.append(names[i])

    debug(time_names)
    # finding common time vector
    for i, name in enumerate(time_names):
        times = data[name]
        times = times[~np.isnan(times)]
        if 0 == i:
            time_set = set(times)
        else:
            time_set = time_set.union(set(times))
    time_vec = np.array(sorted(time_set))

    # extend the data with the time vector
    data = np.copy(recfunctions.append_fields(data, "time", time_vec, dtypes=float,
            usemask=False, fill_value=np.nan, asrecarray=True))

    for i, time in enumerate(time_names):
        para = para_names[i]
        debug("Using variable:" + para)
        times = data[time]
        # Remove invalid times introduced from nan and the extending of the array
        times = times[~np.isnan(times)]
        times = times[np.where(times < 10**19)]

        index_in_new_array = time_vec.searchsorted(times)
        data_to_insert = np.copy(data[0:len(index_in_new_array)][para])
        full_idx = np.arange(len(time_vec))
        nan_idx = np.setdiff1d(full_idx, index_in_new_array)
        debug("NaN indexes = " + str(len(nan_idx)))
        debug("Good indexes = " + str(len(data_to_insert)))
        data[para][nan_idx] = np.nan
        data[para][index_in_new_array[:]] = data_to_insert

    # Remove all the individual time vector since now all has been converted and adjusted to one common
    for time_name in time_names:
        data = recfunctions.drop_fields(data, time_name)

    return data

import matplotlib.mlab as mlab
def add_turbid(data, turbid):
    turbids = np.array([turbid for i in range(len(data))])
    #turbids = np.ndarray((len(turbids), ), buffer=turbids, dtype="|O4" )
    #data = recfunctions.append_fields(data, "turbid", data=turbids, usemask=False)
    data =  mlab.rec_append_fields(data, "turbid", turbids)
    return data


def prepare_data(data, para_describs):
    debug("Preparing data")
    data = _find_common_time_base(data, para_describs)
    return data

if __name__=='__main__':
    import prepare_for_save
    import doctest
    doctest.testmod(prepare_for_save, verbose=False)
